<?php
namespace app\user\controller;

use tp5_lib\user\model\UserModel;
use app\user\service\UserService;
use think\Controller;
use think\facade\Session;

/**
 * 后台管理
 */
class AdminController extends Controller
{
    /**
     * 主页面
     */
    public function index()
    {
        $data=input();
        $um=new UserModel();
        $query=$um->db();
        if(isset($data["uid"])&&$data["uid"]!==""){
            $query->where("id = ".$data["uid"]);
        }
        if(isset($data["username"])&&$data["username"]!==""){
            $query->where("username like '%".$data["username"]."%'");
        }
        $list=$query->paginate(3);
        $page=$list->render();
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch("index");
    }
    /**
     * 将指定用户踢下线
     */
    public function logout()
    {
        $data=input();
        if(!isset($data["id"])){
            $this->error("用户ID不能为空");
        }
        $um=new UserModel();
        $user=$um->where(["id"=>$data["id"]])->find();
        if(!$user){
            $this->error="用户不存在";
            return false;
        }
        Session::ban($user["token"]);
        $this->success("下线成功");
    }
    /**
     * 禁用指定用户
     */
    public function forbidden()
    {
        
    }
    /**
     * 删除指定用户
     */
    public function remove()
    {
        $data=input();
        if(!isset($data["id"])){
            $this->error("用户ID不能为空");
        }
        $ul=new UserService();
        $result=$ul->remove($data["id"]);
        if($result===false){
            $this->error($ul->getError());
        }
        $this->success("删除成功");
    }
}
